# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.10.2)

# Declares and names the project.

project("video_encoder")
ENABLE_LANGUAGE(ASM)
#set( openh264_src_DIR ./third_party/openh264/ )

#set( openh264_build_DIR ./third_party/openh264/outputs )

#file(MAKE_DIRECTORY ${openh264_build_DIR})
#add_subdirectory(${openh264_src_DIR} ${openh264_build_DIR})
#add_library( lib_openh264 STATIC IMPORTED )
#set_target_properties( lib_openh264 PROPERTIES IMPORTED_LOCATION
#        ${openh264_build_DIR}/${ANDROID_ABI}/openh264.a )
#include_directories( ${openh264_src_DIR}/include )

set(SRC_FILES "software_codec.cpp")
list(APPEND SRC_FILES "./third_party/openh264/src/codec/common/src/utils.cpp")
list(APPEND SRC_FILES "./third_party/openh264/src/codec/common/src/crt_util_safe_x.cpp"
"./third_party/openh264/src/codec/common/src/WelsTaskThread.cpp"
"./third_party/openh264/src/codec/common/src/WelsThread.cpp"
"./third_party/openh264/src/codec/common/src/WelsThreadLib.cpp"
"./third_party/openh264/src/codec/common/src/WelsThreadPool.cpp"
"./third_party/openh264/src/codec/common/src/common_tables.cpp"
"./third_party/openh264/src/codec/common/src/copy_mb.cpp"
"./third_party/openh264/src/codec/common/src/cpu.cpp"
"./third_party/openh264/src/codec/common/src/crt_util_safe_x.cpp"
"./third_party/openh264/src/codec/common/src/deblocking_common.cpp"
"./third_party/openh264/src/codec/common/src/expand_pic.cpp"
"./third_party/openh264/src/codec/common/src/intra_pred_common.cpp"
"./third_party/openh264/src/codec/common/src/mc.cpp"
"./third_party/openh264/src/codec/common/src/memory_align.cpp"
"./third_party/openh264/src/codec/common/src/sad_common.cpp"
"./third_party/openh264/src/codec/common/src/utils.cpp"
"./third_party/openh264/src/codec/common/src/welsCodecTrace.cpp"
"./third_party/openh264/src/codec/processing/src/adaptivequantization/AdaptiveQuantization.cpp"
"./third_party/openh264/src/codec/processing/src/backgrounddetection/BackgroundDetection.cpp"
"./third_party/openh264/src/codec/processing/src/common/WelsFrameWork.cpp"
"./third_party/openh264/src/codec/processing/src/common/WelsFrameWorkEx.cpp"
"./third_party/openh264/src/codec/processing/src/common/memory.cpp"
"./third_party/openh264/src/codec/processing/src/complexityanalysis/ComplexityAnalysis.cpp"
"./third_party/openh264/src/codec/processing/src/denoise/denoise.cpp"
"./third_party/openh264/src/codec/processing/src/denoise/denoise_filter.cpp"
"./third_party/openh264/src/codec/processing/src/downsample/downsample.cpp"
"./third_party/openh264/src/codec/processing/src/downsample/downsamplefuncs.cpp"
"./third_party/openh264/src/codec/processing/src/imagerotate/imagerotate.cpp"
"./third_party/openh264/src/codec/processing/src/imagerotate/imagerotatefuncs.cpp"
"./third_party/openh264/src/codec/processing/src/scenechangedetection/SceneChangeDetection.cpp"
"./third_party/openh264/src/codec/processing/src/scrolldetection/ScrollDetection.cpp"
"./third_party/openh264/src/codec/processing/src/scrolldetection/ScrollDetectionFuncs.cpp"
"./third_party/openh264/src/codec/processing/src/vaacalc/vaacalcfuncs.cpp"
"./third_party/openh264/src/codec/processing/src/vaacalc/vaacalculation.cpp"
        "./third_party/openh264/src/codec/encoder/core/src/au_set.cpp"
        "./third_party/openh264/src/codec/encoder/core/src/deblocking.cpp"
        "./third_party/openh264/src/codec/encoder/core/src/decode_mb_aux.cpp"
        "./third_party/openh264/src/codec/encoder/core/src/encode_mb_aux.cpp"
        "./third_party/openh264/src/codec/encoder/core/src/encoder.cpp"
        "./third_party/openh264/src/codec/encoder/core/src/encoder_data_tables.cpp"
        "./third_party/openh264/src/codec/encoder/core/src/encoder_ext.cpp"
        "./third_party/openh264/src/codec/encoder/core/src/get_intra_predictor.cpp"
        "./third_party/openh264/src/codec/encoder/core/src/md.cpp"
        "./third_party/openh264/src/codec/encoder/core/src/mv_pred.cpp"
        "./third_party/openh264/src/codec/encoder/core/src/nal_encap.cpp"
        "./third_party/openh264/src/codec/encoder/core/src/paraset_strategy.cpp"
        "./third_party/openh264/src/codec/encoder/core/src/picture_handle.cpp"
        "./third_party/openh264/src/codec/encoder/core/src/ratectl.cpp"
        "./third_party/openh264/src/codec/encoder/core/src/ref_list_mgr_svc.cpp"
        "./third_party/openh264/src/codec/encoder/core/src/sample.cpp"
        "./third_party/openh264/src/codec/encoder/core/src/set_mb_syn_cabac.cpp"
        "./third_party/openh264/src/codec/encoder/core/src/set_mb_syn_cavlc.cpp"
        "./third_party/openh264/src/codec/encoder/core/src/slice_multi_threading.cpp"
        "./third_party/openh264/src/codec/encoder/core/src/svc_base_layer_md.cpp"
        "./third_party/openh264/src/codec/encoder/core/src/svc_enc_slice_segment.cpp"
        "./third_party/openh264/src/codec/encoder/core/src/svc_encode_mb.cpp"
        "./third_party/openh264/src/codec/encoder/core/src/svc_encode_slice.cpp"
        "./third_party/openh264/src/codec/encoder/core/src/svc_mode_decision.cpp"
        "./third_party/openh264/src/codec/encoder/core/src/svc_motion_estimate.cpp"
        "./third_party/openh264/src/codec/encoder/core/src/svc_set_mb_syn_cabac.cpp"
        "./third_party/openh264/src/codec/encoder/core/src/svc_set_mb_syn_cavlc.cpp"
        "./third_party/openh264/src/codec/encoder/core/src/wels_preprocess.cpp"
        "./third_party/openh264/src/codec/encoder/core/src/wels_task_base.cpp"
        "./third_party/openh264/src/codec/encoder/core/src/wels_task_encoder.cpp"
        "./third_party/openh264/src/codec/encoder/core/src/wels_task_management.cpp"
        "./third_party/openh264/src/codec/encoder/plus/src/welsEncoderExt.cpp"

)

if (${CMAKE_ANDROID_ARCH_ABI} STREQUAL "armeabi-v7a")
list(APPEND SRC_FILES
        "./third_party/openh264/src/codec/common/arm/arm_arch_common_macro.S"
        "./third_party/openh264/src/codec/common/arm/copy_mb_neon.S"
        "./third_party/openh264/src/codec/common/arm/deblocking_neon.S"
        "./third_party/openh264/src/codec/common/arm/expand_picture_neon.S"
        "./third_party/openh264/src/codec/common/arm/intra_pred_common_neon.S"
        "./third_party/openh264/src/codec/common/arm/mc_neon.S"
        "./third_party/openh264/src/codec/encoder/core/arm/intra_pred_neon.S"
        "./third_party/openh264/src/codec/processing/src/arm/adaptive_quantization.S"
        "./third_party/openh264/src/codec/processing/src/arm/down_sample_neon.S"
        "./third_party/openh264/src/codec/processing/src/arm/pixel_sad_neon.S"
        "./third_party/openh264/src/codec/processing/src/arm/vaa_calc_neon.S"
        "./third_party/openh264/src/codec/encoder/core/arm/intra_pred_sad_3_opt_neon.S"
        "./third_party/openh264/src/codec/encoder/core/arm/memory_neon.S"
        "./third_party/openh264/src/codec/encoder/core/arm/pixel_neon.S"
        "./third_party/openh264/src/codec/encoder/core/arm/reconstruct_neon.S"
        "./third_party/openh264/src/codec/encoder/core/arm/svc_motion_estimation.S"
        )
include_directories("./third_party/openh264/src/codec/common/arm")
add_definitions("-DANDROID_NDK -DHAVE_NEON")
elseif(${CMAKE_ANDROID_ARCH_ABI} STREQUAL "arm64-v8a")
    list(APPEND SRC_FILES
            "./third_party/openh264/src/codec/common/arm64/arm_arch64_common_macro.S"
            "./third_party/openh264/src/codec/common/arm64/copy_mb_aarch64_neon.S"
            "./third_party/openh264/src/codec/common/arm64/deblocking_aarch64_neon.S"
            "./third_party/openh264/src/codec/common/arm64/expand_picture_aarch64_neon.S"
            "./third_party/openh264/src/codec/common/arm64/intra_pred_common_aarch64_neon.S"
            "./third_party/openh264/src/codec/common/arm64/mc_aarch64_neon.S"
            "./third_party/openh264/src/codec/processing/src/arm64/adaptive_quantization_aarch64_neon.S"
            "./third_party/openh264/src/codec/processing/src/arm64/down_sample_aarch64_neon.S"
            "./third_party/openh264/src/codec/processing/src/arm64/pixel_sad_aarch64_neon.S"
            "./third_party/openh264/src/codec/processing/src/arm64/vaa_calc_aarch64_neon.S"
            "./third_party/openh264/src/codec/encoder/core/arm64/intra_pred_aarch64_neon.S"
            "./third_party/openh264/src/codec/encoder/core/arm64/intra_pred_sad_3_opt_aarch64_neon.S"
            "./third_party/openh264/src/codec/encoder/core/arm64/memory_aarch64_neon.S"
            "./third_party/openh264/src/codec/encoder/core/arm64/pixel_aarch64_neon.S"
            "./third_party/openh264/src/codec/encoder/core/arm64/reconstruct_aarch64_neon.S"
            "./third_party/openh264/src/codec/encoder/core/arm64/svc_motion_estimation_aarch64_neon.S"
            )
    include_directories("./third_party/openh264/src/codec/common/arm64")
    add_definitions("-DANDROID_NDK -DHAVE_NEON_AARCH64")
endif()

include_directories("./third_party/openh264/src/codec/common/src")
include_directories("./third_party/openh264/src/codec/common/inc")
include_directories("./third_party/openh264/src/codec/api/svc")
include_directories("./third_party/openh264/src/codec/processing/interface")
include_directories("./third_party/openh264/src/codec/processing/src/adaptivequantization")
include_directories("./third_party/openh264/src/codec/processing/src/backgrounddetection")
include_directories("./third_party/openh264/src/codec/processing/src/common")
include_directories("./third_party/openh264/src/codec/processing/src/complexityanalysis")
include_directories("./third_party/openh264/src/codec/processing/src/denoise")
include_directories("./third_party/openh264/src/codec/processing/src/downsample")
include_directories("./third_party/openh264/src/codec/processing/src/imagerotate")
include_directories("./third_party/openh264/src/codec/processing/src/scenechangedetection")
include_directories("./third_party/openh264/src/codec/processing/src/scrolldetection")
include_directories("./third_party/openh264/src/codec/processing/src/vaacalc")
include_directories("./third_party/openh264/src/codec/encoder/core/inc")
include_directories("./third_party/openh264/src/codec/encoder/core/src")
include_directories("./third_party/openh264/src/codec/encoder/plus/inc")
include_directories("./third_party/openh264/src/codec/encoder/plus/src")

add_library(cpufeatures STATIC ${ANDROID_NDK}/sources/android/cpufeatures/cpu-features.c)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
        software_codec

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
        ${SRC_FILES})
target_include_directories(software_codec PRIVATE ${ANDROID_NDK}/sources/android/cpufeatures)
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
              log-lib

              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
        software_codec
        cpufeatures
#        lib_openh264
                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )